package com.linktrip.devicemanager.server;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linktrip.devicemanager.dao.ISMSDao;
import com.linktrip.devicemanager.entity.SMSLog;
import com.linktrip.devicemanager.sms.SMSUtil;
import com.linktrip.devicemanager.util.ToolKits;

@Service(value = "smsService")
@SuppressWarnings("rawtypes")
public class SMSService {

	Logger log = LoggerFactory.getLogger(SMSService.class);

	@Autowired
	ISMSDao smsDao;

	public List<SMSLog> paging(Map map) {
		return smsDao.paging(map);
	}
	
	public int totals(Map map){
		return smsDao.totals(map);
	}

	public boolean sendMsg(String content, String... numbers) {
		boolean reply = false;
		for (String num : numbers) {
			if(ToolKits.isEmpty(num)) continue;
			reply = SMSUtil.send(content, num);
			SMSLog entity = new SMSLog();
			entity.setSms_type("短信");
			entity.setChannel("标准");
			entity.setProvider("CMPP");
			entity.setTarget(num);
			entity.setContent(content);
			entity.setReply(reply ? 1 : 2);
			entity.setCreate_date(System.currentTimeMillis());
			smsDao.add(entity);
		}
		return reply;
	}
	
	public boolean batchSendMsg(String content, String... numbers) {
		boolean reply = false;
		if(numbers == null || numbers.length <= 0)return false;
		reply = SMSUtil.send(content, numbers);
		for (String num : numbers) {
			if(ToolKits.isEmpty(num)) continue;
			SMSLog entity = new SMSLog();
			entity.setSms_type("短信");
			entity.setChannel("标准");
			entity.setProvider("CMPP");
			entity.setTarget(num);
			entity.setContent(content);
			entity.setReply(reply ? 1 : 2);
			entity.setCreate_date(System.currentTimeMillis());
			smsDao.add(entity);
		}
		return reply;
	}
}
